<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 180px;"
        class="filter-item"
        placeholder="请输入账户号"
        v-model="queryForm.acctNo"
      >
      </el-input>
      <el-input
        style="width: 180px;"
        class="filter-item"
        placeholder="请输入用户手机号"
        v-model="queryForm.phone"
      >
      </el-input>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="acctName"
        label="账户名"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="acctNo" label="账户号" width="250" align="center">
      </el-table-column>
      <el-table-column
        prop="balance"
        label="账户余额"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="openTime"
        label="开户时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="isFreeze"
        label="是否冻结"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.IS_TRUE[scope.row.isFreeze] }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        width="300"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'userAccount:detail'"
            size="mini"
            type="default"
            @click="handleQueryById(scope.row, 'published')"
            icon="el-icon-search"
            circle
            title="查看详情"
          >
          </el-button>
          <el-button
            v-permission="'userAccount:acctDetail'"
            size="mini"
            type="success"
            @click="queryById(scope.row, 'published')"
            icon="el-icon-search"
            circle
            title="查看账户详情"
          >
          </el-button>
          <el-button
            v-permission="'userAccount:freeze'"
            size="mini"
            type="danger"
            v-if="scope.row.isFreeze == 0"
            @click="freeze(scope.row, 'published')"
            icon="el-icon-close"
            circle
            title="冻结"
          >
          </el-button>
          <el-button
            v-permission="'userAccount:thaw'"
            size="mini"
            type="primary"
            v-if="scope.row.isFreeze == 1"
            @click="thaw(scope.row, 'published')"
            icon="el-icon-check"
            circle
            title="解冻"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <!-- 详情弹出框 -->
    <el-dialog
      :title="`账户详情`"
      :visible.sync="dialogShowFormVisible"
      width="50%"
    >
      <el-form :model="showForm" label-width="120px" ref="showForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户号:" prop="acctNo">
              {{ showForm.acctNo }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称:" prop="realName">
              {{ showForm.realName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号:" prop="phone">
              {{ showForm.phone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户名称:" prop="acctName">
              {{ showForm.acctName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账户余额:" prop="balance">
              {{ showForm.balance }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户补贴余额:" prop="subsidyBalance">
              {{ showForm.subsidyBalance }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="冻结余额:" prop="freezeAmount">
              {{ showForm.freezeAmount }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开户时间:" prop="openTime">
              {{ showForm.openTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销户时间:" prop="closeTime">
              {{ showForm.closeTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="冻结时间:" prop="freezeTime">
              {{ showForm.freezeTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否冻结:" prop="isFreeze">
              <template slot-scope="scope">
                {{ Dicts.IS_TRUE[showForm.isFreeze] }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShowFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="`查看账户详情`"
      :visible.sync="detailFormVisible"
      width="65%"
    >
      <el-table v-loading="loading" :data="detailRecords" style="width: 100%">
        <el-table-column prop="voucherCode" label="业务单号" width="200">
        </el-table-column>
        <el-table-column prop="originAmount" label="账户原金额" width="100">
        </el-table-column>
        <el-table-column prop="tradeAmount" label="交易金额" width="100">
        </el-table-column>
        <el-table-column prop="presentAmount" label="当前账户余额" width="120">
        </el-table-column>
        <el-table-column prop="changeDir" label="改变方向" width="100">
          <template slot-scope="scope">
            {{ Dicts.CHANGE_DIR[scope.row.changeDir] }}
          </template>
        </el-table-column>
        <el-table-column prop="tradeDesc" label="交易说明" width="200">
        </el-table-column>
        <el-table-column prop="transName" label="交易类型名" width="100">
        </el-table-column>
        <el-table-column prop="createTime" label="交易开始时间" width="150">
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="queryDetailForm.current"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="queryDetailForm.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="queryDetailForm.total"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryUserAccount,
  selectUserAccountDetail,
  selectIntrAcctBookDetail,
  editUserAccount
} from '@/api/account'

export default {
  name: 'userAccount',
  data() {
    return {
      loading: false,
      dialogShowFormVisible: false,
      detailFormVisible: false,
      showForm: [],
      detailRecords: [],
      queryDetailForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        importance: undefined,
        title: undefined
      },
      exampleForm: {},
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        importance: undefined,
        title: undefined
      },
      transCodeOptions: Dicts.TRANS_CODE,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryUserAccount(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleQueryById({ rowId }) {
      selectUserAccountDetail(rowId).then(({ data: { code, data, msg } }) => {
        if (code === 0) {
          this.openShowDialog()
          this.showForm = data
        }
      })
    },
    queryById({ rowId }) {
      this.queryDetailForm = {}
      this.queryDetailForm.acctId = rowId
      selectIntrAcctBookDetail(this.queryDetailForm).then(
        ({ data: { data } }) => {
          this.detailFormVisible = true
          this.queryDetailForm.current = data.current
          this.queryDetailForm.pages = data.pages
          this.queryDetailForm.total = data.total
          this.queryDetailForm.size = data.size
          this.detailRecords = data.records
          this.loading = false
        }
      )
    },
    openShowDialog() {
      this.showForm = {}
      this.dialogShowFormVisible = true
      this.$refs.showForm && this.$refs.showForm.resetFields()
    },
    currentChange(current) {
      this.queryDetailForm.current = current
      this.queryUserAccountDetailById(this.queryDetailForm.acctId)
    },
    sizeChange(size) {
      this.queryDetailForm.size = size
      this.queryUserAccountDetailById(this.queryDetailForm.acctId)
    },
    queryUserAccountDetailById(rowId) {
      this.loading = true
      this.queryDetailForm.acctId = rowId
      selectIntrAcctBookDetail(this.queryDetailForm).then(
        ({ data: { data } }) => {
          this.detailFormVisible = true
          this.queryDetailForm.current = data.current
          this.queryDetailForm.pages = data.pages
          this.queryDetailForm.total = data.total
          this.queryDetailForm.size = data.size
          this.detailRecords = data.records
          this.loading = false
        }
      )
    },
    freeze({ rowId }) {
      this.exampleForm.rowId = rowId
      this.exampleForm.isFreeze = 1
      this.$confirm('是否冻结该账户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editUserAccount(this.exampleForm).then(
          ({ data: { data, msg, code } }) => {
            if (code === 0) {
              this.doQuery()
            }
            this.$message({
              showClose: true,
              message: msg,
              type: code === 0 ? 'success' : 'error'
            })
          }
        )
      })
    },
    thaw({ rowId }) {
      this.exampleForm.rowId = rowId
      this.exampleForm.isFreeze = 0
      this.$confirm('是否解冻该账户?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        editUserAccount(this.exampleForm).then(
          ({ data: { data, msg, code } }) => {
            if (code === 0) {
              this.doQuery()
            }
            this.$message({
              showClose: true,
              message: msg,
              type: code === 0 ? 'success' : 'error'
            })
          }
        )
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
